<template>
  <a-modal v-model:open="formVisible" :title="formData.id ? '编辑合同' : '添加合同'" width="1000" :footer="null" destroy-on-close @cancel="hideForm">
    <div class="contract-form-container">
      <a-form ref="formRef" :model="formData" :rules="rules" layout="vertical" class="contract-form">
        <a-row :gutter="[16, 16]">
          <!-- 合同编号 -->
          <a-col :span="6">
            <a-form-item name="contractNo" label="合同编号" class="form-bottom-line">
              <a-input v-model:value="formData.contractNo" placeholder="请输入合同编号" />
            </a-form-item>
          </a-col>

          <!-- 合同相对方 -->
          <a-col :span="6">
            <a-form-item name="counterparty" label="合同相对方" class="form-bottom-line">
              <a-input v-model:value="formData.counterparty" placeholder="请输入合同相对方" />
            </a-form-item>
          </a-col>

          <!-- 合同标的物 -->
          <a-col :span="6">
            <a-form-item name="subject" label="合同标的物" class="form-bottom-line">
              <a-input v-model:value="formData.subject" placeholder="请输入合同标的物" />
            </a-form-item>
          </a-col>

          <!-- 规格 -->
          <a-col :span="6">
            <a-form-item name="specification" label="规格" class="form-bottom-line">
              <a-input v-model:value="formData.specification" placeholder="请输入规格" />
            </a-form-item>
          </a-col>

          <!-- 合同单价（元、含税） -->
          <a-col :span="6" class="form-bottom-line">
            <a-form-item name="unitPriceTaxIncluded" label="合同单价（元、含税）" class="form-bottom-line">
              <a-input-number v-model:value="formData.unitPriceTaxIncluded" placeholder="请输入单价" />
            </a-form-item>
          </a-col>

          <!-- 合同单价（元、不含税） -->
          <a-col :span="6">
            <a-form-item name="unitPriceTaxExcluded" label="合同单价（元、不含税）" class="form-bottom-line">
              <a-input-number v-model:value="formData.unitPriceTaxExcluded" placeholder="请输入单价" />
            </a-form-item>
          </a-col>

          <!-- 合同签订时间 -->
          <a-col :span="6">
            <a-form-item name="signDate" label="合同签订时间" class="form-bottom-line">
              <a-input v-model:value="formData.signDate" placeholder="请选择签订时间" />
            </a-form-item>
          </a-col>

          <!-- 合同有效期 -->
          <a-col :span="6">
            <a-form-item name="validityPeriod" label="合同有效期" class="form-bottom-line">
              <a-input v-model:value="formData.validityPeriod" placeholder="请输入合同有效期" />
            </a-form-item>
          </a-col>

          <!-- 计量单位 -->
          <a-col :span="6">
            <a-form-item name="unit" label="计量单位" class="form-bottom-line">
              <a-input v-model:value="formData.unit" placeholder="请输入计量单位" />
            </a-form-item>
          </a-col>

          <!-- 合同数量 -->
          <a-col :span="6">
            <a-form-item name="quantity" label="合同数量" class="form-bottom-line">
              <a-input-number v-model:value="formData.quantity" placeholder="请输入数量" />
            </a-form-item>
          </a-col>

          <!-- 合同金额（万元，不含税） -->
          <a-col :span="6">
            <a-form-item name="amountTaxExcluded" label="合同金额（万元，不含税）" class="form-bottom-line">
              <a-input-number v-model:value="formData.amountTaxExcluded" placeholder="请输入金额" :precision="2" />
            </a-form-item>
          </a-col>

          <!-- 剩余采购量 -->
          <a-col :span="6">
            <a-form-item name="remainingPurchaseQuantity" label="剩余采购量" class="form-bottom-line">
              <a-input-number v-model:value="formData.remainingPurchaseQuantity" placeholder="请输入剩余采购量" :precision="4" />
            </a-form-item>
          </a-col>

          <!-- 执行量（吨） -->
          <a-col :span="6">
            <a-form-item name="executedQuantity" label="执行量（吨）" class="form-bottom-line">
              <a-input-number v-model:value="formData.executedQuantity" placeholder="请输入执行量" :precision="4" />
            </a-form-item>
          </a-col>

          <!-- 合同执行率% -->
          <a-col :span="6">
            <a-form-item name="executionRate" label="合同执行率%" class="form-bottom-line">
              <a-input v-model:value="formData.executionRate" placeholder="请输入执行率" />
            </a-form-item>
          </a-col>

          <!-- 月份 -->
          <a-col :span="6">
            <a-form-item name="month" label="月份" class="form-bottom-line">
              <a-date-picker v-model:value="formData.month" placeholder="请选择月份" format="YYYY-MM" picker="month" />
            </a-form-item>
          </a-col>
        </a-row>

        <div class="form-footer">
          <a-button type="primary" @click="handleSubmit">保存</a-button>
          <a-button style="margin-left: 10px" @click="hideForm">取消</a-button>
        </div>
      </a-form>
    </div>
  </a-modal>
</template>

<script setup>
import { formModel } from './data'

defineOptions({ name: 'ContractForm' })

const emit = defineEmits(['success'])

const formRef = ref(null)
const formVisible = ref(false) // 弹框显示
const formData = reactive({}) // 表单数据
// 验证规则
const rules = {
  contractNo: [{ required: true, message: '请输入合同编号', trigger: 'blur' }],
  counterparty: [{ required: true, message: '请输入合同相对方', trigger: 'blur' }],
  subject: [{ required: true, message: '请输入合同标的物', trigger: 'blur' }],
  specification: [{ required: true, message: '规格', trigger: 'blur' }],
  signDate: [{ required: true, message: '请选择合同签订时间', trigger: 'change' }],
  unitPriceTaxIncluded: [{ required: true, message: '请输入合同单价（含税）', trigger: 'blur' }],
  month: [{ required: true, message: '请选择月份', trigger: 'blur' }],
}

/**
 * 取消
 */
function hideForm() {
  formVisible.value = false
  Object.assign(formData, {})
}

// 提交表单
async function handleSubmit() {
  await formRef.value.validate()
  await httpRequest(`/admin/contract/${formData.id ? 'update' : 'add'}`, formData)
  message.success('保存成功')
  emit('success')
  formVisible.value = false
}

/**
 * 编辑
 * @param record
 */
function edit(record) {
  if (record) {
    const dateKeys = ['signDate', 'month']
    for (const key in formModel) {
      if (dateKeys.includes(key)) {
        formData[key] = record[key] ? dayjs(record[key]) : null
      } else {
        formData[key] = record[key]
      }
    }
  } else {
    Object.assign(formData, { ...formModel })
  }
  formVisible.value = true
}

// 暴露方法供父组件使用
defineExpose({ edit })
</script>

<style lang="less" scoped>
@import '@/assets/style/resetForm.less';

.contract-form-container {
  width: 100%;
}

.contract-form {
  margin-top: 20px;
}

.form-footer {
  margin-top: 30px;
  text-align: right;
}
</style>
